package com;

import android.text.TextUtils;

import com.baselibrary.utils.GsonUtils;
import com.github.lzyzsd.jsbridge.BridgeWebView;
import com.github.lzyzsd.jsbridge.CallBackFunction;
import com.hot.BridgeConstant;
import com.hot.JsDispatchCallBack;
import com.hot.JsSignDispatchCallBack;

/**
 * @author 关忆北丶
 * @description:
 * @date :2020/9/19 14:57
 */
public class BridgeWebViewUtils {

    private BridgeWebView mWebView;

    public BridgeWebViewUtils(BridgeWebView mWebView) {
        this.mWebView = mWebView;
    }

    /**
     * 发送参数给H5
     * @param jsMethodName
     * @param data
     */
    public void sendJsData(String jsMethodName, String data, CallBackFunction callBack) {
        if (callBack != null)
            mWebView.callHandler(jsMethodName, data, callBack);
        else
            mWebView.callHandler(jsMethodName, data, data1 -> {
            });
    }

    public void registerHandler(JsDispatchCallBack callBack) {
        mWebView.registerHandler(BridgeConstant.DISTRIBUTEMES, (data, function) -> {
            if (TextUtils.isEmpty(data) || callBack == null)
                return;
            JSData mJsData = GsonUtils.parseJsonToBean(data, JSData.class);
            if (mJsData == null || mJsData.getParam() == null)
                return;
            int mFunc = mJsData.getParam().getFunc();
            switch (mFunc) {
                case 1://传Token 给 H5
                    callBack.getToken(function);
                    break;
                case 2:
                    callBack.getHaiBao(function);
                    break;
                case 3:
                    callBack.onBack(function);
                    break;
                case 4:
                    callBack.onDialog(function, mJsData.getParam().getContent());
                    break;
                case 5://H5 传token 给 客户端
                    callBack.loadH5Token(mJsData.getParam().getDesc(), function);
                    break;
                case 6:
                    callBack.loadObject(mJsData.getParam().getDesc(), function);
                    break;
            }
        });
    }

    public void registerHandler(JsSignDispatchCallBack callBack) {
        mWebView.registerHandler(BridgeConstant.DISTRIBUTEMES, (data, function) -> {
            if (TextUtils.isEmpty(data) || callBack == null)
                return;
            JSData mJsData = GsonUtils.parseJsonToBean(data, JSData.class);
            if (mJsData == null || mJsData.getParam() == null)
                return;
            JSData.ParamBean mParam = mJsData.getParam();
            if (mParam == null)
                return;
            int mFunc = mParam.getFunc();
            switch (mFunc) {
                case 1://传Token 给 H5
                    callBack.getToken(function);
                    break;
                case 3://关闭h5页面
                    callBack.onBack(function);
                    break;
                case 100://标定签到
                    callBack.appointMap(function, mParam);
                    break;
                case 101://跳转到群聊
                    callBack.jumpGroup(function, mParam);
                    break;
                case 102://拨打电话
                    callBack.callPhone(function, mParam);
                    break;
                case 103://问卷填写结束
                    callBack.fillQuestionnaireEnd(function, mParam);
                    break;
                case 104://选择问卷界面按钮高亮
                    callBack.questionnaireBtn(function, mParam);
                    break;
                case 105://获取NFC溯源防伪数据
                    callBack.getNfcData(function, mParam);
                    break;
                case 106://'手动获取设备信息及token
                    callBack.getDeviceInfo(function, mParam);
                    break;
                case 110://打开客服
                    callBack.openAppServer(function, mParam);
                    break;
                case 404://重新登陆
                    callBack.reLogin(function, mParam);
                    break;
                case 107://选择省市区
                    callBack.selectAddress(function, mParam);
                    break;
                case 108://获取当前定位
                    callBack.getCurrentAddress(function, mParam);
                    break;
                case 109://获取App当前默认地址
                    callBack.getAppHarvestAddress(function, mParam);
                    break;
                case 111://客户端添加地址并返回web回显
                    callBack.addAddress(function, mParam);
                    break;
                case 112://跳转地址列表选择地址
                    callBack.selectReceivingAddress(function, mParam);
                    break;
                case 113://会员登记完成点击提交
                    callBack.registerOver(function, mParam);
                    break;
                case 114://通知客户端调用获取酒币接口
                    callBack.loadWineCoin(function, mParam);
                    break;
                case 115: //跳转话题详情
                    callBack.jupmTopicDetail(function, mParam);
                    break;
                case 116: //领取弹窗
                    callBack.showActiveRecivePop(function, mParam);
                    break;
                case 117: //跳转到动态详情(0,文本，1.图文，2.视频，4转发)
                    callBack.jumpDynamicDetail(function, mParam);
                    break;
                case 118://金鳞瓶立即抢购
                    callBack.jumpJinLinRushBuy(function, mParam);
                    break;
                case 11801://打开金鳞瓶活动规则  页面禁止下拉刷新的意思
                    callBack.openOrCloseRulePop(function, mParam,true);
                    break;
                case 11802://关闭金鳞瓶活动规则   页面可下拉刷新
                    callBack.openOrCloseRulePop(function, mParam,false);
                    break;
                case 119://金鳞瓶去支付（已下单未支付）
                    callBack.jumpJinLinRushPay(function, mParam);
                    break;
                case 121://金鳞分享
                    callBack.goJinlinShare(function, mParam);
                    break;
                case 122://友盟埋点
                    callBack.UMBp(function, mParam);
                    break;
                case 201://打开订单详情
                    callBack.jumpOrderDetail(function, mParam);
                    break;
                case 203://打开售后订单详情
                    callBack.jumpAfterOrderDetail(function, mParam);
                    break;
                case 130://文化大使投票 分享海报弹窗
                    callBack.showSharePoster4oteRank(function, mParam);
                    break;
                case 204://左上角返回（客户端）上一页
                    callBack.backPage(function, mParam);
                    break;
                case 205://告知客户端去除顶部
                    callBack.hideTopTitle(function, mParam);
                    break;
                case 206://告知客户端显示顶部
                    callBack.showTopTitle(function, mParam);
                    break;
                case 207://打开搜索酒窖
                    callBack.jumpSearchWineCellar(function, mParam);
                    break;
                case 208://打开APP路由
                    callBack.jumpAPPRouter(function, mParam);
                    break;
                case 209://获取用户信息
                    callBack.getCurrentUserInfo(function, mParam);
                    break;
                case 210://禁用侧滑返回
                    callBack.banBack(function, mParam);
                    break;
                case 211://请求相机权限
                    callBack.openCameraPermission(function, mParam);
                    break;
                case 212://跳转商品详情
                    callBack.openGoodsDetail(function, mParam);
                    break;
                case 11803://信息栏调整为深色
                    callBack.setStatusBarDark(function,mParam);
                    break;
                case 213://用户关联埋点
                    callBack.getUid(function);
                    break;
            }
        });
    }
}
